import { _decorator, Component, Node } from 'cc';
import { AudioManager } from '../../../common/src/manager/AudioManager';
import { assetManager } from 'cc';
import { director } from 'cc';
import { sys } from 'cc';
import { UIManager } from '../../../framework/ui/uimanager/UIManager';
import { sp } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('StartCtrl')
export class StartCtrl extends Component {
    
    @property({ type: sp.Skeleton, displayName: "动画" })
    anim: sp.Skeleton;
    
    private bundleVer: string

    start() {
        this.bundleVer = sys.localStorage.getItem('__bundleVersionConfig__');
    }

    update(deltaTime: number) {
        
    }

    // -------------------- 事件 -------------------- //
    // 设置按钮
    private async settingClicked() {
        AudioManager.playButton()
        console.log ("点击设置")
        await UIManager.open("setting:res/prefab/SettingPanel")
    }
    
    // 排行榜按钮
    private rankClicked() {
        AudioManager.playButton()
        console.log ("点击排行榜")
    }
    
    // 分享按钮
    private shareClicked() {
        AudioManager.playButton()
        window.gameSdk.sdk.shareAppMessage({
            positionTag:"分享标记点位",
            callback: (err, res) => {
                if (!err) {
                    console.log("分享成功");
                }else{
                    console.log("分享失败");
                }
            }
        });
        console.log ("点击分享")
    }
    
    // 皮肤按钮
    private skinClicked() {
        AudioManager.playButton()
        console.log ("点击皮肤")
    }
    
    // 设置按钮
    private gameBeginClicked() {
        this.anim.node.active = true
        this.anim.setAnimation(0, "animation", false)

        director.addPersistRootNode(this.anim.node)

        AudioManager.playButton()
        assetManager.loadBundle('game', { version: this.bundleVer }, (err, bundle) => {
            if (err) { return gg.error(err); }
            
            bundle.loadScene('game', (err, scene) => {
                if (err) { return gg.error(err); }
                director.runScene(scene);
            });
        });
    }
}


